﻿using MediatR;
using W.Warehouse.Domain;
using W.Warehouse.Read.Api.Application.Command.WarehouseCommands;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder
{
    public class WarehouseGetTypesCommandHandlder : IRequestHandler<WarehouseGetTypesCommand, ApiResult<List<WarehouseType>>>
    {
        private readonly ILogger<WarehouseGetTypesCommandHandlder> _logger;
        private readonly IWarehouseTypeRepository _warehouseTypeRepository;
        public WarehouseGetTypesCommandHandlder(IWarehouseTypeRepository warehouseTypeRepository, ILogger<WarehouseGetTypesCommandHandlder> logger)
        {
            _warehouseTypeRepository = warehouseTypeRepository;
            _logger = logger;
        }
        public async Task<ApiResult<List<WarehouseType>>> Handle(WarehouseGetTypesCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<WarehouseType>> result = new ApiResult<List<WarehouseType>>();
            try
            {
                _logger.LogInformation("显示仓库类型成功");
                var list = await _warehouseTypeRepository.GetAsync(x => x.IsDelete == false);
                result.Data = list;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
